/*登录界面*/
(function($){
	var LOGIN = {
		init: function () {
			this.domElem();
			this.events();
			this.validate();
			//this.changeValidate();
		},
		domElem: function () {
			this.self = $('#shop_login_form');
			this.username = this.self.find('input[name="username"]');
			this.password = this.self.find('input[name="password"]');
			this.checkbox = this.self.find('input[name="remPas"]');
			//this.validcode = this.self.find('input[name="validcode"]');
			//this.capsLock = this.self.find('.js-capsLock');
			//this.avoidLogin = this.self.find('.js_c_radio');
			this.submitBtn = this.self.find('.js_submit');
		},
		events: function () {
			var that = this;

			$(window).keydown(function(e) {
				if (e.keyCode === 13) {
					that.submitBtn.click();
				}
			});

			that.password.keypress(function(e) {
				if (that.checkCapsLock(e)) {
					that.capsLock.show();
				} else {
					that.capsLock.hide();
				}
			});
			/*
			that.avoidLogin.click(function() {
				if ($(this).hasClass('on')) {
					$(this).removeClass('on');
					that.checkbox.prop('checked', false);
				} else {
					$(this).addClass('on');
					that.checkbox.prop('checked', true);
				}
			});
			*/
		},
		/*
		checkCapsLock: function (event) {
			var capsLockKey = event.keyCode ? event.keyCode : event.which;
			var shifKey = event.shiftKey ? event.shiftKey:((capsLockKey == 16) ? true : false);

			if (((capsLockKey >= 65 && capsLockKey <= 90) && !shifKey)||((capsLockKey >= 97 && capsLockKey <= 122) && shifKey)) {
				return true;
			} else {
				return false;
			}
		},*/
		validate: function () {
			var that = this;
			var flag = true;

			that.submitBtn.click(function() {
				setTimeout(function(){
					that.self.submit();
				},0);
				alert(that.self);
			});
			
			that.self.submit(function() {
				that.username.blur();
				that.password.blur();
                that.validcode.blur();
				if (flag) {
                    that.submitBtn.find('b').html('登 录 中...')
				} else {
                    return false;
                }
			});

			var error_message = {
				'username_blank':'请输入登录名',
				'username_min_length':'用户名长度不能低于5个字符',
				'username_max_length':'用户名长度不能超出26个字符',
				'password_blank':'请输入密码',
				'password_min_length':'密码不能少于6位',
				'validate_blank':'请输入验证码',
				'validate_equals':'验证码必须4位',
				'validate_error':'验证码错误',
				'validate_get_error':'获取验证码错误'
			};

			that.username.focus(function() {
				$(this).parent().addClass('form_row_focus');
			});

			that.username.blur(function() {
				var value = $.trim($(this).val());
				$(this).parent().removeClass('form_row_focus');
				if (value.length === 0) {
					$(this).parent().addClass('form_row_error');
					$(this).parent().find('.validation_marked_info').html(error_message.username_blank);
					flag = false;
				} else if (value.length > 25) {
					$(this).parent().addClass('form_row_error');
					$(this).parent().find('.validation_marked_info').html(error_message.username_max_length);
					flag = false;
//				} else if (value.length < 5) {
//					$(this).parent().addClass('form_row_error');
//					$(this).parent().find('.validation_marked_info').html(error_message.username_min_length);
//					flag = false;
				} else {
					$(this).parent().removeClass('form_row_error');
					$(this).parent().find('.validation_marked_info').html('');
					flag = true;
				}
			});

			that.password.focus(function() {
				$(this).parent().addClass('form_row_focus');
			});

			that.password.blur(function() {
				var value = $.trim($(this).val());
				$(this).parent().removeClass('form_row_focus');
				if (value.length === 0) {
					$(this).parent().addClass('form_row_error');
					$(this).parent().find('.validation_marked_info').html(error_message.password_blank);
					flag = false;
				} else if (value.length < 6) {
					$(this).parent().addClass('form_row_error');
					$(this).parent().find('.validation_marked_info').html(error_message.password_min_length);
					flag = false;
				} else {
					$(this).parent().removeClass('form_row_error');
					$(this).parent().find('.validation_marked_info').html('');
					flag = true;
				}
			});
			/*
			if (that.validcode.length) {
				that.validcode.focus(function() {
					$(this).parent().addClass('form_row_focus');
				});

				that.validcode.blur(function() {
					var value = $.trim($(this).val());

					$(this).parent().removeClass('form_row_focus');
					if (value.length === 0) {
						$(this).parent().addClass('form_row_error');
						$(this).parent().find('.validation_marked_info').html(error_message.validate_blank);
                        flag = false;
					} else if (value.length < 4) {
						$(this).parent().addClass('form_row_error');
						$(this).parent().find('.validation_marked_info').html(error_message.validate_equals);
                        flag = false;
					} else {
						$.get("getValidCode", {
						  random: Math.random(),
						  validcode: value
						}, function(result) {
						  if (result.success) {
						  	$(this).parent().remove('form_row_error');
						   	$(this).parent().find('.validation_marked_info').html('');
						   	flag = true;
						  } else {
						    that.refreshValidate();
						    $(this).parent().find('.validation_marked_info').html(error_message.validate_error);
						    flag = false;
						  }
						});
					}
				});
			}
			*/

		},
		changeValidate: function () {
			var that = this;
			$("#changValidate").click(function(){
				that.refreshValidate();
			});
		},
		refreshValidate: function () {
			setTimeout(function(){
				$("#verifyCodeId").attr("src","getKaptcha3?random="+Math.random());
			},1);
		}
	};

	LOGIN.init();
}(jQuery));